package mapper.log;

import infobean.log;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import dbDrivers.db2Connection;

import mapper.dbinterface;

public class db2MapperLog {
	public static dbDrivers.db2islemleri db=new dbDrivers.db2islemleri();
	public static boolean DataBaseDeleteObject(Object oid) throws SQLException {
		log log = (log) oid;
		String sql = "DELETE FROM KEYSAN.\"LOGLAR\" WHERE LOG_ID="+log.getLog_id();
		return db.execute(sql);
	}

 
	public static ArrayList DataBaseGetObject(Object oid) {
		// TODO Auto-generated method stub
		
		
		String sql = null ;
		ArrayList<log> geri_loglar = new ArrayList<log>();
		sql= "SELECT * FROM KEYSAN.\"LOGLAR\" L, KEYSAN.\"AYGITTURU\" AT, KEYSAN.\"KULLANC\" K1, KEYSAN.\"KULLANC\" K2 WHERE K1.KULLANICI_ID=L.KULLANICI_ID AND K2.KULLANICI_ID=L.BAGLANILAN_KULLANICI_ID AND AT.AYGIT_SERI_NO=L.AYGIT_SERI_NO";	
		
		if (oid == null || oid.getClass() == String.class ){
			sql= "SELECT * FROM KEYSAN.\"LOGLAR\" L, KEYSAN.\"AYGITTURU\" AT, KEYSAN.\"KULLANC\" K1, KEYSAN.\"KULLANC\" K2 WHERE K1.KULLANICI_ID=L.KULLANICI_ID AND K2.KULLANICI_ID=L.BAGLANILAN_KULLANICI_ID AND AT.AYGIT_SERI_NO=L.AYGIT_SERI_NO";	
		}else if(oid.getClass() == log.class  ){
			log log = (log) oid;
			if (log.getAygit_seri_no() != 0 ){
				if (sql.indexOf("WHERE") != -1){
					sql += " AND L.AYGIT_SERI_NO="+log.getAygit_seri_no();
					
				}
				else{
					sql += " WHERE L.AYGIT_SERI_NO="+log.getAygit_seri_no();
				}
				
			}if (log.getCozum() != null){
				if (sql.indexOf("WHERE") != -1){
					sql += " AND L.COZUM='"+log.getCozum()+"'";
					
				}
				else{
					sql += " WHERE L.COZUM='"+log.getCozum()+"'";
				}
				
			}if (log.getHata() != null){
				if (sql.indexOf("WHERE") != -1){
					sql += " AND L.HATA='"+log.getHata()+"'";
					
				}
				else{
					sql += " WHERE L.HATA='"+log.getHata()+"'";
				}
				
			}if (log.getKullanici().getKullanici_id() != 0){
				if (sql.indexOf("WHERE") != -1){
					sql += " AND L.KULLANICI_ID="+log.getKullanici().getKullanici_id();
					
				}
				else{
					sql += " WHERE L.KULLANICI_ID="+log.getKullanici().getKullanici_id();
				}
				
			}if (log.getLog_id() != 0){
				if (sql.indexOf("WHERE") != -1){
					sql += " AND L.LOG_ID="+log.getLog_id();
					
				}
				else{
					sql += " WHERE L.LOG_ID="+log.getLog_id();
				}
				
			}if (log.getTarih() != null){
				if (sql.indexOf("WHERE") != -1){
					sql += " AND L.TARIH="+log.getTarih();
					
				}
				else{
					sql += " WHERE L.TARIH="+log.getTarih();
				}
				
			}if (log.getIp() != null){
				if (sql.indexOf("WHERE") != -1){
					sql += " AND L.IP="+log.getIp();
					
				}
				else{
					sql += " WHERE L.IP="+log.getIp();
				}
				
			}if (log.getYapilan_islem() != null){
				if (sql.indexOf("WHERE") != -1){
					sql += " AND L.YAPILAN_ISLEM='"+log.getYapilan_islem()+"'";
					
				}
				else{
					sql += " WHERE L.YAPILAN_ISLEM='"+log.getYapilan_islem()+"'";
				}
				
			}if (log.getBaglanilan_kullanici().getKullanici_id() != 0){
				if (sql.indexOf("WHERE") != -1){
					sql += " AND L.BAGLANILAN_KULLANICI_ID="+log.getBaglanilan_kullanici().getKullanici_id();
					
				}
				else{
					sql += " WHERE L.BAGLANILAN_KULLANICI_ID="+log.getBaglanilan_kullanici().getKullanici_id();
				}
				
			}
		}
		System.out.println(sql);
		ResultSet rs = db.executeQuery(sql);
		if (rs != null){
			System.out.println("hededede");
		try {
			while(rs.next())
			{

				 System.out.print("\nHEHE ");
				 try {
					 log gelen_log = new log();
					 
					 gelen_log.getKullanici().setKullanici_id(rs.getLong("KULLANICI_ID"));
					 gelen_log.getBaglanilan_kullanici().setKullanici_id(rs.getLong("BAGLANILAN_KULLANICI_ID"));
					 gelen_log.getBaglanilan_kullanici().setGroup(rs.getInt("GRUP"));
					 gelen_log.getKullanici().setGroup(rs.getInt("GRUP"));
					 gelen_log.setLog_id(rs.getLong("LOG_ID"));
					 gelen_log.setAygit_seri_no(rs.getLong("AYGIT_SERI_NO"));
					 gelen_log.setTarih(rs.getDate("TARIH"));
					 gelen_log.setIp(rs.getString("IP"));
					 gelen_log.setYapilan_islem(rs.getString("YAPILAN_ISLEM"));
					 gelen_log.setHata(rs.getString("HATA"));
					 gelen_log.setCozum(rs.getString("COZUM"));
					 gelen_log.setAygit_id(rs.getLong("AYGIT_ID"));
					 gelen_log.setAygit_turu(rs.getString("AYGIT_TURU"));
					 geri_loglar.add(gelen_log);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}}else{
			System.out.println("Resultset boşşş");
		}
		return geri_loglar;
		//return null;
	}

 
	public static boolean DataBasePut(Object oid) throws SQLException {
		log log = (log) oid;
		// TODO Auto-generated method stub
		String sql = "insert into KEYSAN.\"LOGLAR\" (KULLANICI_ID, AYGIT_SERI_NO," +
		" TARİH, IP, YAPILAN_ISLEM, BAGLANILAN_KULLANICI, HATA, COZUM) values ("+
		log.getKullanici().getKullanici_id()+","+log.getAygit_seri_no()+","+
		log.getTarih()+",'"+log.getIp()+"','"+log.getYapilan_islem()+"',"+log.getBaglanilan_kullanici().getKullanici_id()+
		",'"+log.getHata()+"','"+log.getCozum()+"')";
		return db.execute(sql);
		
	}

	 
	public static boolean DataBaseUpdate(Object oid) { // log update i olmaz
		// TODO Auto-generated method stub
		return false;
	}

}
